//
//  B_SettingViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/14.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_SettingViewController.h"
#import "B_ReleaseFooterView.h"
#import "B_SettingTableViewCell.h"

#import "B_ChangeDataVC.h"
#import "B_ChangePasswordVC.h"
#import "B_ManagerAddressVC.h"
#import "B_AboutUsVC.h"

@interface B_SettingViewController ()


@property (nonatomic, strong) BaseTableView * mainView;

@property (nonatomic, strong) B_ReleaseFooterView * logoutView;

@end

@implementation B_SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"设置"];
    self.dataArray = [NSMutableArray arrayWithObjects:@"修改个人资料", @"修改密码", @"我的收货地址", @"关于我们", @"当前版本", nil];
    self.view = self.mainView;
    
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView addSubview:self.logoutView];
        [self.logoutView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(80));
        }];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (80 + 24) : 80;
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_SettingTableViewCell" bundle:nil] forCellReuseIdentifier:@"B_SettingTableViewCell"];
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 60;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_SettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"B_SettingTableViewCell" forIndexPath:indexPath];
            cell.B_titleLabel.text = weSelf.dataArray[indexPath.row];
            if (indexPath.row == weSelf.dataArray.count - 1) {
                cell.B_otherLabel.text =  getAppVersion();
                cell.B_otherLabel.hidden = NO;
            } else {
                cell.B_otherLabel.hidden = YES;
            }
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.row == 0) {
                B_ChangeDataVC * data = [[B_ChangeDataVC alloc] init];
                [weSelf pushNextViewController:data];
            } else if (indexPath.row == 1) {
                B_ChangePasswordVC * password = [[B_ChangePasswordVC alloc] init];
                [weSelf pushNextViewController:password];
            } else if (indexPath.row == 2) {
                B_ManagerAddressVC * address = [[B_ManagerAddressVC alloc] init];
                [weSelf pushNextViewController:address];
                
            } else if (indexPath.row == 3) {
                B_AboutUsVC * aboutUs = [[B_AboutUsVC alloc] init];
                [weSelf pushNextViewController:aboutUs];
            }
        };
        
    }
    return _mainView;
}

- (B_ReleaseFooterView *)logoutView {
    if (!_logoutView) {
        _logoutView = [B_ReleaseFooterView initB_ReleaseFooterView];
        [_logoutView.B_SureButton setTitle:@"确认退出" forState:UIControlStateNormal];
        _logoutView.sureActionBlock = ^{
            //TODO:执行退出操作
            logout();
        };
    }
    return _logoutView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
